<?php
Class Group{
public static function Add(){
	$status = array('content'=>NULL,'color'=>"red");
	
	$request = getRequest::getInstance();
    $result = array();     
				if($request->isPost()==1){
					$post = $request->postParams();

					if(!empty($post['name'])){
						$orm = ORM::factory('groups');
						$orm->role = $post['name'];

						$RAZDEL = "0";
                                                $MEDIA = "0";
                                                $USERS = "0";
                                                $GROUPS = "0";
                                                $cpanel = "0";
						if(isset($post['cpanel'])){
                                                    $cpanel = $post['cpanel'];
                                                    if(!empty($post['RAZDEL'])){
                                                      $RAZDEL = $post['RAZDEL'];
                                                    }


                                                    if(!empty($post['MEDIA'])){
                                                      $MEDIA = $post['MEDIA'];
                                                    }


                                                    if(!empty($post['USERS'])){
                                                        if($post['USERS'] >=5 && !empty($post['GROUPS'])){
                                                                 $GROUPS = $post['USERS'];
                                                        }
                                                    $USERS = $post['USERS'];
                                                    }
                                                }
                                                    $orm->cpanel = $cpanel;
                                                    $orm->flush();
                                                    $role = $orm->return_id();
                                                    
                                                    unset($orm);

                                                    ORM::factory()->prepare("INSERT INTO acl (`Role`, `Modul`, `Rights`) VALUES('".$role."','RAZDEL','".$RAZDEL."'), ('".$role."','MEDIA','".$MEDIA."'), ('".$role."','USERS','".$USERS."'), ('".$role."','GROUPS','".$GROUPS."')")->execute();

						$status['content'] = "Група успешно добавлена";
						$status['color'] = "green";
					}else{
						$status['content'] = "Вы не ввели одно из обязательных полей";
					}
					
					
				}else{
                                      $privilegies = ORM::factory('groups')->prepare("SELECT privilegies_name.id, privilegies_name.name, privilegies_name.modul, privilegies_type.mod, privilegies_type.name FROM privilegies_type LEFT JOIN privilegies_name ON privilegies_name.id=privilegies_type.priv")->getResult();
                                         foreach($privilegies as $key=>$value){
                                             if(!isset($result[$value[1]])){
                                                 $result[$value[1]] = array();
                                                 $result[$value[1]]['modul'] = $value['modul'];
                                             }
                                             if(!isset($result[$value[1]][$value['mod']])){
                                                 $result[$value[1]][$value['mod']]['name'] = $value[1];
                                                 $result[$value[1]][$value['mod']]['type'] = $value['name'];
                                                 $result[$value[1]][$value['mod']]['mod'] = $value['mod'];
                                            }
                                         }
                                }
return array('result'=>$result, 'status'=>$status);
}

public static function Edit(){
	$status = array('content'=>NULL,'color'=>"red");
	$priv = array();
	$request = getRequest::getInstance();
	
                $id = $request->getVar("id", "GET");
                
                $role = ORM::factory()->prepare("SELECT * FROM `groups` WHERE `id`='".$id."'")->getResult();
              
		$title = "Редактирование групы \"".$role[0]['role']."\"";

                $privilegies = ORM::factory()->prepare("SELECT privilegies_name.id, privilegies_name.name, privilegies_name.modul, privilegies_type.mod, privilegies_type.name FROM privilegies_type LEFT JOIN privilegies_name ON privilegies_name.id=privilegies_type.priv")->getResult();
                $my_privilegies = ORM::factory('acl')->SetType("assoc")->Where("Role", $id)->find_all();
                $i=0;
                   foreach($privilegies as $key=>$value){
                          if(!isset($priv[$value[1]])){
                               $priv[$value[1]] = array();
                               $priv[$value[1]]['modul'] = $value['modul'];

                               if($my_privilegies[$i]['Modul'] == $value['modul']){
                                 $priv[$value[1]]['Rights'] = $my_privilegies[$i]['Rights'];
                               }

                               $i++;
                          }
                          if(!isset($priv[$value[1]][$value['mod']])){
                               $priv[$value[1]][$value['mod']]['name'] = $value[1];
                               $priv[$value[1]][$value['mod']]['type'] = $value['name'];
                               $priv[$value[1]][$value['mod']]['mod'] = $value['mod'];
                               
                               
                          }
                          
                     }

		if($request->isPost()==1){
                        $post = $request->postParams();
                        if(!empty($post['name'])){
                                $role = $post['id'];
                                                $RAZDEL = "0";
                                                $MEDIA = "0";
                                                $USERS = "0";
                                                $GROUPS = "0";
                                                $cpanel = "0";
                                    if(isset($post['cpanel'])){
                                         $cpanel = $post['cpanel'];
                                         if(!empty($post['RAZDEL'])){
                                              $RAZDEL = $post['RAZDEL'];
                                         }
                                         if(!empty($post['MEDIA'])){
                                              $MEDIA = $post['MEDIA'];
                                         }
                                         if(!empty($post['USERS'])){
                                                if($post['USERS'] >=5 && !empty($post['GROUPS'])){
                                                   $GROUPS = $post['USERS'];
                                                }
                                         $USERS = $post['USERS'];
                                         }
                                             
                                    }
                                             ORM::factory()->prepare("UPDATE `groups` SET groups.role = '".$post['name']."', groups.cpanel='".$cpanel."' WHERE groups.id='".$role."'")->execute();

                                             ORM::factory()->prepare("UPDATE `acl` SET acl.Rights = '".$RAZDEL."' WHERE acl.Modul='RAZDEL' AND acl.Role='".$role."'")->execute();
                                             ORM::factory()->prepare("UPDATE `acl` SET acl.Rights = '".$MEDIA."' WHERE acl.Modul='MEDIA' AND acl.Role='".$role."'")->execute();
                                             ORM::factory()->prepare("UPDATE `acl` SET acl.Rights = '".$USERS."' WHERE acl.Modul='USERS' AND acl.Role='".$role."'")->execute();
                                             ORM::factory()->prepare("UPDATE `acl` SET acl.Rights = '".$GROUPS."' WHERE acl.Modul='GROUPS' AND acl.Role='".$role."'")->execute();
			}
                       header("location: /admin/groups.html");
		}

return array('priv'=>$priv,'role'=>$role,'title'=>$title,'status'=>$status);
}
}